//
//  ProvinceViewController.m
//  10_Work 省市区
//
//  Created by dllo on 16/7/26.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "ProvinceViewController.h"
#define REUSE @"reuse"
#import "CityViewController.h"

@interface ProvinceViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *proArray;

@property (nonatomic, strong) NSMutableArray *array;
@end

@implementation ProvinceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSString *areaStr = [NSString stringWithContentsOfFile:@"/Users/dllo/Desktop/10_Work 省市区/10_Work 省市区/area.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *arr = [areaStr componentsSeparatedByString:@"\n"];
    self.array = [NSMutableArray arrayWithArray:arr];
    self.proArray = [NSMutableArray array];
    for (NSString *str in self.array) {
        if (![str hasPrefix:@" "]) {
            NSMutableArray *cityArray = [NSMutableArray array];
            NSMutableDictionary *proDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:str, @"省名", cityArray, @"市数组", nil];
            [self.proArray addObject:proDic];
        } else if (![str hasPrefix:@"    "]) {
            NSMutableArray *cityArray = [NSMutableArray array];
            cityArray = [[self.proArray lastObject] objectForKey:@"市数组"];
            NSMutableArray *districtArray = [NSMutableArray array];
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:str, @"市名", districtArray, @"区数组", nil];
            [cityArray addObject:cityDic];
        } else {
            NSMutableArray *districtArray = [NSMutableArray array];
            NSMutableArray *cityArray = [[self.proArray lastObject] objectForKey:@"市数组"];
            districtArray = [[cityArray lastObject] objectForKey:@"区数组"];
            [districtArray addObject:str];
        }
    }
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:REUSE];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.proArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:REUSE];
    cell.textLabel.text = [self.proArray[indexPath.row] objectForKey:@"省名"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CityViewController *city = [[CityViewController alloc] init];
    city.cityDic = self.proArray[indexPath.row];
    [self.navigationController pushViewController:city animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
